const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

//转换为日期格式
function formatDate(time) {
  var date = new Date(time);
  var YY = date.getFullYear() + '-';
  var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  // return YY + MM + DD + " " + hh + mm + ss;
  return YY + MM + DD ;
}

//获取当前时间 2019-02-12 12:00:00
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
  return currentdate;
}

//今天以前yyyy-MM-dd  今天 hh:mm
function formatDateTime(dateStr) {
  dateStr = dateStr.replace(/\-/g, "/");
  var date = new Date(dateStr);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  var currentDate = new Date();
  //当前年
  var currentYear = currentDate.getFullYear()
  //当前月
  var currentMonth = currentDate.getMonth() + 1
  //当前日
  var currentDay = currentDate.getDate()

  //需要格式化的时间，就是当天
  if (year == currentYear && month == currentMonth && day == currentDay)
  {
    return  [hour, minute].map(formatNumber).join(':')
  }
  else
  {
    return [year, month, day].map(formatNumber).join('-')
  }
}

function getDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

function getTime(date) {
  var hour = date.getHours()
  var minute = date.getMinutes()
  return [hour, minute].map(formatNumber).join(':')
}

//添加缓存数据
function setStorage(key,data){
  wx.setStorage({
    key: key,
    data: data
  })
}

//获取缓存数据
function getStorage(key, callback){
  wx.getStorage({
    key: key,
    success: function (res) {
      callback(res.data);
    }, fail: function (res) {
      callback(null);
    }
  })
}

//删除缓存数据
function removeStorage(key)
{
  wx.removeStorage({
    key: key,
    success: function (res) {
    }
  })
}

//清空缓存
function clearStorage(){
  wx.clearStorage();
}

//写日志
function logInfo(str){
  getApi(
    "up/Supplier/LogText",
    {data:str},
    function (res) {
    }
  ) 
}

//克隆
function cloneObj(oldObj) { //复制对象方法
  if (typeof (oldObj) != 'object') return oldObj;
  if (oldObj == null) return oldObj;
  var newObj = new Object();
  for (var i in oldObj)
    newObj[i] = cloneObj(oldObj[i]);
  return newObj;
}

function extendObj() { //扩展对象
  var args = arguments;
  if (args.length < 2) return;
  var temp = cloneObj(args[0]); //调用复制对象方法
  for (var n = 1; n < args.length; n++) {
    for (var i in args[n]) {
      temp[i] = args[n][i];
    }
  }
  return temp;
} 

function doctorTypeToStr(typeId) {
  var doctorTypeStr = [
    "骨科",
    "心理科",
    "耳科",
    "咽喉科",
    "心血管科",
    "泌尿科",
    "呼吸科",
    "消化科",
    "口腔科",
    "儿科",
    "妇科",
    "神经科",
  ];

  return doctorTypeStr[typeId];
}

function doctorStrToInt(str) {
  var doctorTypeStr = [
    "骨科",
    "心理科",
    "耳科",
    "咽喉科",
    "心血管科",
    "泌尿科",
    "呼吸科",
    "消化科",
    "口腔科",
    "儿科",
    "妇科",
    "神经科",
  ];

  for (var i = 0; i < doctorTypeStr.length; i++) {
    if (str == doctorTypeStr[i]) {
      return i;
    }
  }
  return 0;
}

function doctorLevelToStr(level) {
  var doctorLevelStr = [
    "教授",
    "副教授",
    "主任医师",
    "副主任医师",
    "主治医师",
    "副主治医师",
  ];

  return doctorLevelStr[level];
}

function doctorLevelStrToInt(str) {
  var doctorLevelStr = [
    "教授",
    "副教授",
    "主任医师",
    "副主任医师",
    "主治医师",
    "副主治医师",
  ];

  for (var i = 0; i < doctorLevelStr.length; i++) {
    if (str == doctorLevelStr[i]) {
      return i;
    }
  }
  return 0;
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  getNowFormatDate: getNowFormatDate,
  formatDateTime: formatDateTime,
  getDate: getDate,
  getTime: getTime,
  setStorage: setStorage,
  getStorage: getStorage,
  removeStorage: removeStorage,
  clearStorage: clearStorage,
  logInfo: logInfo,
  extendObj: extendObj,

  doctorTypeToStr: doctorTypeToStr,
  doctorStrToInt: doctorStrToInt,
  doctorLevelToStr: doctorLevelToStr,
  doctorLevelStrToInt: doctorLevelStrToInt
}
